package com.itheima.job;

import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * @author zhengjd
 * @date 2020/4/16 20:23
 */
public class CleanOrderSetting {

    @Autowired
    OrderSettingDao orderSettingDao;

    /**
     * 项目实战-定时任务
     * 删除预约设置表的历史数据
     */
    public void deleteOrderSettingHistoryData(){
        try {
            //获取执行任务时的日期
            Date date = new Date();
            String cleanDay = DateUtils.parseDate2String(date);
            //String cleanDay = "2019-04-05";
            System.out.println("时间："+cleanDay+"，正在执行了删除预约设置表的历史数据");
            //删除之前，将要删除的历史数据添加到t_ordersetting_history表中，存放历史数据
            List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingListByDate(cleanDay);
            if (orderSettingList != null && orderSettingList.size()>0){
                for (OrderSetting orderSetting : orderSettingList) {
                    //往历史数据表中添加数据
                    orderSettingDao.addHistory(orderSetting);
                }
                //执行完之后删除主表的历史数据
                orderSettingDao.deleteHistoryData(cleanDay);
            }
            System.out.println("删除预约设置表历史数据任务完成");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
